//
//  FunctionButtons.m
//  myselfTest
//
//  Created by Lucifer on 15/10/21.
//  Copyright © 2015年 xuxzhou. All rights reserved.
//

#import "FunctionButtons.h"
#import "UIColor+Helper.h"
#define rowNumber 4
#define buttonTags 100
@implementation FunctionButtons

-(instancetype)initWithFrame:(CGRect)frame RowNum:(NSInteger )rowCount{
    self=[super initWithFrame:frame];
    if (self) {
        _rowNum=rowCount;
    }
    return self;

}
-(void)initializeUI{
    _rowNum=rowNumber;
   _lineNum = _itemsArray.count/rowNumber +(_itemsArray.count%rowNumber==0?0:1);
    for (int i =0; i<_itemsArray.count; i++) {
        UIButton * button=[UIButton buttonWithType:UIButtonTypeCustom];
        button.frame=CGRectMake(i%_rowNum*( CGRectGetWidth(self.frame)/_rowNum), i/_rowNum*(CGRectGetHeight(self.frame)/_lineNum), CGRectGetWidth(self.frame)/_rowNum, CGRectGetHeight(self.frame)/_lineNum);
        button.backgroundColor=[UIColor colorWithRed: 84 / 255.0 green: 110 / 255.0 blue: 167 / 255.0 alpha: 0.4];
        button.layer.borderColor=[[UIColor whiteColor]CGColor];
        button.layer.borderWidth=0.5;
        button.tag=buttonTags+i;
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:[NSString stringWithFormat:@"%@",_itemsArray[i]] forState:UIControlStateNormal];
        [self addSubview:button];
    }
}
-(void)buttonClicked:(UIButton *)button{
    self.FunctionButtonClickedBlock(button.tag-100);
}
-(void)setItemsArray:(NSArray *)itemsArray{
    _itemsArray=itemsArray;
    [self initializeUI];
}
-(void)setRowNum:(NSInteger)rowNum{
    _rowNum=rowNum;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
